var util = require("../../../utils/util.js")
const gd = getApp().globalData;
const net = require('../../../utils/net.js');
var posting = false;
var dialog = null;
var calendar = null;
var calendar2 = null;

Page({
  data: {
    list: "",
    month: "",
    day: "",
    search_month: "",
    months: "",
    month: 2,
    rili: "",
    days: "",
    tipdate: "",
    tindate: "今天",
    mons: "",
    mon: "",
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    index: 0,
    date: '2018',
    tipdate: "",
  },
toclick(){
},
dayonebindDateChange: function (e) {
  var gbdate = (e.detail.value).slice(0, 4);
  var rilis = e.detail.value;
  var rili = gbdate + "-" + rilis.substring(5);
  var ri = rilis.substring(5);
  var toda = ri.slice(3, 5);
  this.setData({
    date: gbdate,
    toda,
    mon: (e.detail.value).slice(5, 7),
    rili: rili
  })
  calendar.handleCalendar(this.data.date, e.detail.value.slice(5, 7));
  setTimeout(function () { calendar.pointDate([e.detail.value]) }, 300);
  var cTab = this.data.currentTab;
  if (cTab==0){
    calendar.handleCalendar(this.data.date, e.detail.value.slice(5, 7));
    setTimeout(function () { calendar.pointDate([e.detail.value]) }, 300);
    this.todate()
  }
  
  else if (cTab == 1){
    
    var objcet = this.riqi();
    var year = this.data.date;
    var mon = this.data.mon;
    var now = this.data.rili;
    console.log(now)
    var date = now.slice(8, 10);
    console.log(date);
    var thisweke = calendar2.thisWeek(year + "", mon + "", date + "");
    console.log(thisweke)
    calendar2.handleCalendar(year,mon);
    setTimeout(function () { calendar2.lineDate(thisweke) }, 100);
    var start = thisweke[0];
    var end = thisweke[6];
    this.todate(start,end);
  } else if (cTab == 2){
    var obj = this.yue()
    var date = new Date();
    var year =this.data.date;
    var Start = year + "-" + this.data.mon + "-" + obj.one;
    var end = year + "-" + this.data.mon + "-" + obj.last;

    // var year = date.getFullYear();
    var Start = year + "-" + this.data.mon + "-" + obj.one;
    var end = year + "-" + this.data.mon + "-" + obj.last;
    console.log(year);
    calendar.handleCalendar(this.data.date, e.detail.value.slice(5, 7));
    setTimeout(function () {calendar2.lineDate(thisweke); }, 100)
    this.todate(Start, end)
  }
  else{
    this.tipswichNav()
    this.todate(start, end);
  }
},
zhou(){
  var objcet = this.riqi();

  var year = this.data.date;
  var mon = this.data.mon;
  var now = this.data.rili

  var date = now.substring(8);
  var thisweke = calendar2.thisWeek(year + "", mon + "", date + "");
  setTimeout(function () {
    calendar2.lineDate(thisweke);
  }, 100)
  var start = thisweke[0];
  var end = thisweke[6];
  var obje = { sta: start, en:end}
  console.log(obje)
  return obje
},
  // 日期的月绑定事件
  /**
   * 生命周期函数--监听页面加载
   */
  // 日历

  mGetDate(month) {
    var date = new Date();
    var year = date.getFullYear();
    var d = new Date(year, month, 0);
    return d.getDate();
  },
  // 日期天数的点击事件
  tochangeDay:function(e,a,b) {
  var now=new Date();
    var year = this.data.date;
    var mon=this.data.mon;
    var tian = e.detail.cents;
    // var tian = tians < 10 ? "0" + tians:tians
     var rili = year + "-" + mon + "-" + (tian<10?"0"+ tian:tian);
    calendar.handleCalendar(this.data.date, this.data.mon,function(){
      calendar.pointDate([rili]);
    });
    this.setData({
      tian,
      rili: rili
    })
    var cTab = this.data.currentTab;
    if (cTab == 0) {
      this.todate()
    }

    else if (cTab == 1) {
      var objcet = this.riqi();
      var year = this.data.date;
      var mon = this.data.mon;
      var now = this.data.rili;
      console.log(now)
      var date = now.slice(8, 10);
      console.log(date);
      var thisweke = calendar2.thisWeek(year + "", mon + "", date + "");
      console.log(thisweke)
      calendar.handleCalendar(year,mon);
      setTimeout(function () {  calendar2.lineDate(thisweke);}, 100)
      var start = thisweke[0];
      var end = thisweke[6];
      this.todate(start, end);
    }


  },

  //调用日历
 //今天的日期
  bindtap: function (e) {
    var date = new Date();
    var month = date.getMonth() + 1;

    var year = date.getFullYear();
    var daye = date.getDate();
    console.log(daye)
    if (month < 10) {
      month = "0" + month
    }
    var date = new Date().getFullYear() + "-" + month + "-" + (daye<10?"0"+daye:daye);
    var to = this.data.toda;
    console.log(date)
    this.setData({
      date: year,
      mon: month,
      to: daye,
      rili: date
    })
 
    calendar.handleCalendar(year, month);
    setTimeout(function () { calendar.pointDate([date]) }, 300);
    this.todate()

  },
  onebindtap:function(e){
    var dates = new Date();
    var month = new Date().getMonth() + 1;
    var year = dates.getFullYear();
    var daye = dates.getDate();
    if (month < 10) {
      month = "0" + month
    }
    // var date = new Date().getFullYear() + "-" + month + "-" + daye;
    // var objcet = this.riqi();
    // var year = this.data.date;
     //var mon = this.data.mon;
    // var now = this.data.rili;
    // console.log(now);
    // var date = now.slice(8, 10);
    //  console.log(date);
    var date = year+"-"+month+"-"+daye
    var thisweke = calendar2.thisWeek(year + "", month+ "", daye + "");
    calendar2.handleCalendar(year, month);
    setTimeout(function () {
      calendar2.lineDate(thisweke);
    }, 100)
    console.log(thisweke);
    var start = thisweke[0];
    var end = thisweke[6];
    console.log(start);
    console.log(end);
 
    calendar2.handleCalendar(year, month);

    setTimeout(function () {
      calendar2.lineDate(calendar2.thisWeek(year + "", month + "", daye + ""));
    }, 100)
    this.setData({
      date: year,
      mon: month,
      rili: date,
      daye,
      
    })
    console.log(this.data.rili);
    this.todate(start, end)
  },
  twobindtap:function(){
    var obj = this.yue()
    var date = new Date();
    var mon = date.getMonth() + 1;
    if (mon < 10) {
      mon = "0" + mon
    }
    var year = date.getFullYear();
    var dato = date.getDate()
    var shijian = year + "-" + mon + "-" + dato ;
    console.log(shijian);
    var start = year + "-" + mon + "-" + obj.one;
    var end = year + "-" + mon + "-" + obj.last;
    this.setData({
      date: year,
      mon: mon,
      rili: shijian
    })
    this.todate(start, end);
  },
  //  接口调用
  todate(start, end) {
    var that = this;

    net.requestTread({
      url: '/getTreadmillRecord',
      data: {
        shop_id: gd.shop_id,
        member_id: gd.id,
        start_date: start || that.data.rili,
        end_date: end || that.data.rili,
      },
      success: function (res) {
        if (res.data.flag) {
          // dialog.showDialog({
          //   msg: res.data.msg
          // })
          that.setData({
            datadetail: res.data.data
          })

        }
        else {
          dialog.showDialog({
            msg: res.data.msg
          })
        }
      }
    })
  },
  onLoad: function (options) {

    var date = new Date();
    var tia=date.getDate();
    var months = [];
    var ss = date.getMonth() + 1;
    var mon = ss < 10 ? "0" + ss : ss
    var mons = ss < 10 ? "0" + ss : ss;
    var day = date.getDate();
    for (let i = 1; i <= 12; i++) {
      months.push(i)
      months[i] = [];
      for (let j = 1; j <= this.mGetDate(i); j++) {
        months[i].push(j)
      }
    }

    var date = new Date();
    var month = date.getMonth() + 1;
    this.setData({
      search_month: date.getFullYear() + "-" + (month > 9 ? month : "0" + month),
      month,
      months,
      mon,
      tian: tia,
      day
    })
    //日历
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });

  },
  //月
  // 我是月份的切换
  yue() {
    var now = new Date(); //当前日期 
    var nowDayOfWeek = now.getDay(); //今天本周的第几天 
    var nowDay = now.getDate(); //当前日 
    var nowMonth = now.getMonth()+1; //当前月 
    var nowYear = now.getYear(); //当前年 
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
    //日历的区间
    function getMonthDays(myMonth) {
      var monthStartDate = new Date(nowYear, myMonth, 1);
      var monthEndDate = new Date(nowYear, myMonth+1, 1);
      var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
      return days;
    }
    var StartDate = monthStartDate.toString().slice(8, 10)
    var monthEndDate = monthEndDate.toString().slice(8, 10)
    console.log(StartDate);
    console.log(monthEndDate );
    var obj = { one: StartDate, last: monthEndDate }
    return obj
  
  },

  active: function (e) {
    console.log(this.data.date);
    console.log(parseInt(e.target.dataset.current));
    if (this.data.date == 2018 && parseInt(e.target.dataset.current) < 5) {
      dialog.showDialog({
        msg: '2018小于5月不能点击',
      })
      return
    }
    var obj = this.yue()
    var Start = this.data.date + "-" + e.target.dataset.current + "-" + obj.one;
    var end = this.data.date + "-" + e.target.dataset.current + "-" + obj.last;
    console.log(Start);
    console.log(end);
    var that = this;
    if (this.data.mon === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        rili: Start,
        mon: e.target.dataset.current,
        Start,
        end
      })
    }
 
    that.todate(Start, end)
  },
  changeDay(e, option) {
    console.log(option);
    this.setData({
      search_month: e.detail.year + "-" + (e.detail.month > 9 ? e.detail.month : "0" + e.detail.month),
      month: e.detail.month
    })

  },
  //本月
  riqi() {
   
    var now=this.data.rili;
    var now = new Date();
    var nowMonth = now.getMonth() + 1; //当前月 
    var year = now.getFullYear() //当前年 
    var nowdate = now.getDate();
    return getStartEnd(year, nowMonth, nowdate);

    // var year=now.slice(0,4);
    // var nowMonth = now.slice(5,7);
    // var nowdate = now.slice(8,10);
    // return getStartEnd(year, nowMonth, nowdate);
    /**
     * 获取某天所在一周的开始时间和结束时间
     * @param date 日期    2018-05-11
     * */
    function getStartEnd(y, m, d) {
      var now = new Date(y, (m - 1), d);
      var nowDayOfWeek = now.getDay();//周几
      var weekStartDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() - nowDayOfWeek + 1); //开始日期
      var weekEndDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (7 - nowDayOfWeek)); //结束日期
      var see = toYMD(weekStartDate)
      var sen = toYMD(weekEndDate)
      var objcet = { sta: see, en: sen }
      return objcet

    }
    function toYMD(date) {
      var d = new Date(date.getTime());
      var dd = d.getDate() < 10 ? "0" + d.getDate() : d.getDate().toString();
      var mmm = (d.getMonth() + 1) < 10 ? "0" + (d.getMonth() + 1) : (d.getMonth() + 1).toString();
      var yyyy = d.getFullYear().toString(); //2011  
      return yyyy + "-" + mmm + "-" + dd;
    }

  },
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });
    var that = this;
    that.setData({ currentTab: e.detail.current });
    if (e.detail.current == 0) {
      var year=this.data.date;
      var mon=this.data.mon;
      var objcet = this.riqi();
      var now = this.data.rili;
   
      var getData= now.slice(8, 10)
      var rili = year + "-" + mon + "-" + getData ;
      console.log("点击日 getData：" + rili);
      calendar.handleCalendar(year, mon)
      setTimeout(function () { calendar.pointDate([rili]) }, 300);
        
        this.setData({
          rili:rili,
        
        })
      this.todate();
    }
    else if (e.detail.current == 1) {
    var now=new Date().getMonth
      var objcet = this.riqi();
      var year = this.data.date;
      var mon = this.data.mon;
        var now = this.data.rili;
        // var riliyear= now.getFullYear()
        // var riliMonth= now.getMonth()+1;
        // var rilidate =now.getDate()
        // var nos = riliyear + "-" + riliMonth + "-"+rilidate;
        console.log(now);
      //   var dates = now.getDate();
      //   console.log(date);
      // var date=dates;
        var date = now.slice(8,10);
  
      var thisweke = calendar2.thisWeek(year + "", mon + "", date   + "");
         calendar2.handleCalendar(year, mon);
      setTimeout(function () {
        calendar2.lineDate(thisweke);
      }, 100)
      var start = thisweke[0];
      var end = thisweke[6];
      console.log(start);
      console.log(end);
      this.todate(start, end) 
      
    }
    else if (e.detail.current == 2) {
      var obj = this.yue()
      var date = new Date();
      var year = this.data.date;
      var Start = year + "-" + this.data.mon + "-" + obj.one;
      var end = year + "-" + this.data.mon + "-" + obj.last;
      // this.swi(e)
      this.todate(Start, end);
    }
    else if (e.detail.current == 3) {
      var date = new Date();
      var year = date.getFullYear();
      var zmonth = date.getMonth() + 1;
      var date = date.getDate();
      var time = year + "-" + (zmonth < 10 ? "0" + zmonth : zmonth) + "-" + date;
      console.log(time);
      var start = "1970-01-01"
      var end = time;
      var that = this;
      // that.swi(e)
      this.todate(start, end);
    }


  },
  swichNav(e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //我是总
  tipswichNav() {

    var date = new Date();
    var year = date.getFullYear();
    var zmonth = date.getMonth() + 1;
    var date = date.getDate();
    var time = year + "-" + (zmonth < 10 ? "0" + zmonth : zmonth) + "-" + date;
    console.log(time);
    var start = "1970-01-01"
    var end = time;
    var that = this;
    that.swichNav(e)
 
  },

  /**
   * 
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    this.setData({
      imgLeft: (screenWidth - 135)/2
    })

    dialog = this.selectComponent("#dialog");
    calendar = this.selectComponent("#calendar");
    calendar2 = this.selectComponent("#calendar2");

    var date = new Date();
    var year = date.getFullYear()
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
      month = "0" + month
    }
    if (day < 10) {
      day = "0" + day
    }
    var rili = year + "-" + month + "-" + day;


    // var rili ="2018-04-24"
    this.setData({
      rili
    })
    var that = this;
    calendar.handleCalendar(year, month);
    setTimeout(function () { calendar.pointDate([rili]) }, 300);
    this.todate()

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})